%%
%% %CopyrightBegin%
%%
%% Copyright Hillside Technology Ltd. 2016. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%

%% this file was generated by soap:wsdl2erlang, but modified: the body of the 
%% `do_test` function was added and some optional callbacks were added.

%% generated by soap from: e:/e_soap/soap/test/soap_SUITE_data/test_service.wsdl
%% for service "test_service" and port "test_port"
%% using options: [{service,"test_service"},{port,"test_port"},{generate,both},{namespaces,[{"test",undefined}]},{http_server,soap_server_cowboy},{server_name,"test_service_server"},{http_client,soap_client_ibrowse},{client_name,"test_service_client"}]

-module(test_service_server_2).

-include("test_service.hrl").

-export([do_test/3]).
-export([interface/0]).
-export([exception/7]).
-export([check_soap_conformance/2]).

%% This service uses 
%% basic authentication, and this call back is used to check it.
-spec check_soap_conformance(soap:soap_req(), soap:soap_handler_state()) ->  
      soap:soap_handler_response(any()) | 
      {continue, soap:soap_req(), soap:soap_handler_state()}.
check_soap_conformance(Soap_req, State) ->  
    Cowboy_req = soap_req:server_req(Soap_req),
    case soap_req:server(Soap_req) of
        cowboy_1 ->
            {Auth_header, Req2} = cowboy_req:header(<<"authorization">>, Cowboy_req),
            Soap_req2 = soap_req:set_server_req(Soap_req, Req2);
         cowboy_2 -> 
             Auth_header = cowboy_req:header(<<"authorization">>, Cowboy_req),
             Soap_req2 = Soap_req,
             Req2 = Cowboy_req
    end,
    case is_valid_user(Auth_header) of
        true ->
            {continue, Soap_req2, State};
        false ->
            New_cowboy_req =
                cowboy_req:set_resp_header(<<"WWW-Authenticate">>, 
                                           <<"Basic realm=\"Soap\"">>,
                                           Req2),
            {error, 401, soap_req:set_server_req(Soap_req2, New_cowboy_req), State}
    end.

-spec do_test(Parsed_body::request_body(),
    Soap_req::soap:soap_req(), State::soap:soap_handler_state())
    -> soap:soap_handler_response(response_body()).
do_test(#request_body{expected_response="ok"}, Soap_req, State) ->
    {ok, #response_body{response="authenticated!"}, Soap_req, State}.

%% replace the default exception handler to include a stacktrace.
-spec exception(Class::atom(), Reason::any(), Stacktrace::any(),
                soap:soap_fault_code(), Description::string(), soap:soap_req(), 
                soap:soap_handler_state()) -> soap:soap_handler_response().
exception(Class, Reason, Stacktrace, Type, _Desc, Soap_req, Handler_state) ->
    Message = io_lib:format("exception, class: ~p, reason: ~p,~nstack: ~P~n", 
                            [Class, Reason, Stacktrace, 14]),
    {fault, soap_fault:fault(Type, Message, Soap_req), Soap_req, Handler_state}. 

is_valid_user(<<"user:pwd">>) ->
  true;
is_valid_user(_) ->
  false.

%% The 'interface()' function is used by the SOAP framework to access information about
%% the WSDL.
interface() ->
    ?INTERFACE.
